using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using BENBUN.Properties;

namespace BENBUN.Settings
{
    internal class Settings
    {
        private static readonly string prefsPath = (Environment.GetEnvironmentVariable("APPDATA") + "/" + Resources.PrefDirectory + "/preferences.xml");
        private static string _forumUsername;
        private static string _forumPassword;
        private static Boolean _WIWPrivacy;
        private static Boolean _WIWEnabled;
        private static Boolean _WIWAutoLoad;
        private static Boolean _WIWConfirmation;
        private static Boolean _WIWModeTest;

        static Settings()
        {

            if (!load())
            {
                Directory.CreateDirectory(Environment.GetEnvironmentVariable("APPDATA") + "/\\" + Resources.PrefDirectory);
                reset();
                save();
            }
        }

        public static void save()
        {
            XmlDocument document = new XmlDocument();
            XmlElement newChild = document.CreateElement("WIW");
            document.AppendChild(newChild);
            XmlElement element2 = document.CreateElement("settings");
            newChild.AppendChild(element2);
            element2.SetAttribute("forumUsername", _forumUsername);
            element2.SetAttribute("forumPassword", _forumPassword);
            element2.SetAttribute("WIWPrivacy", _WIWPrivacy.ToString() );
            element2.SetAttribute("WIWEnabled", _WIWEnabled.ToString());
            element2.SetAttribute("WIWConfirmation", _WIWConfirmation.ToString());
            element2.SetAttribute("WIWAutoLoad", _WIWAutoLoad.ToString());
            element2.SetAttribute("WIWMode", _WIWModeTest.ToString());

            XmlTextWriter w = new XmlTextWriter(prefsPath, Encoding.UTF8);
            w.Formatting = Formatting.Indented;
            w.Indentation = 3;
            w.IndentChar = ' ';
            document.WriteContentTo(w);
            w.Close();
        }

        private static bool load()
        {
            if (!File.Exists(prefsPath))
            {
                return false;
            }
            XmlDocument document = new XmlDocument();
            XmlReader reader = new XmlTextReader(prefsPath);
            document.Load(reader);
            //
            // Read in the original settings
            //
            try
            {
                XmlNode node = document.ChildNodes[0]["settings"];
                _forumUsername = node.Attributes["forumUsername"].Value;
                _forumPassword = node.Attributes["forumPassword"].Value;
                _WIWPrivacy = bool.Parse(node.Attributes["WIWPrivacy"].Value);
                _WIWModeTest = bool.Parse(node.Attributes["WIWMode"].Value);
                _WIWEnabled = bool.Parse(node.Attributes["WIWEnabled"].Value);
                _WIWConfirmation = bool.Parse(node.Attributes["WIWConfirmation"].Value);
                _WIWAutoLoad = bool.Parse(node.Attributes["WIWAutoLoad"].Value);
                
            }
            catch (Exception)
            {
                reader.Close();
                return false;
            }
            reader.Close();
            return true;
        }


        public static void reset()
        {
            _forumUsername = "";
            _forumPassword = "";
            _WIWPrivacy = false;
            _WIWModeTest = false;
        }

        public static string ForumUsername {
            get {return _forumUsername; }
            set {
                _forumUsername = value;
                save();
            }
        }

        public static string ForumPassword   {
            get { return _forumPassword; }
            set {
                _forumPassword = value;
                save();
            }
        }

        public static Boolean WIWPrivacy {
            get { return _WIWPrivacy; }
            set {
                _WIWPrivacy = value;
                save();
            }
        }

        public static Boolean WIWEnabled
        {
            get { return _WIWEnabled; }
            set
            {
                _WIWEnabled = value;
                save();
            }
        }

        public static Boolean WIWConfirmation
        {
            get { return _WIWConfirmation; }
            set
            {
                _WIWConfirmation = value;
                save();
            }
        }

        public static Boolean WIWModeTest
        {
            get { return _WIWModeTest; }
            set
            {
                _WIWModeTest = value;
                save();
            }
        }
        public static Boolean WIWAutoLoad
        {
            get { return _WIWAutoLoad; }
            set
            {
                _WIWAutoLoad = value;
                save();
            }
        }

        public static string URLBaseAPI
        {
            get
            {
                if (_WIWModeTest)
                {
                    return BENBUN.Properties.Resources.apiBaseTest;
                }
                else
                {
                    return BENBUN.Properties.Resources.apiBaseLive;
                }
            }
        }

        public static string URLBaseWeb
        {
            get
            {
                if (_WIWModeTest)
                {
                    return BENBUN.Properties.Resources.webBaseTest;
                }
                else
                {
                    return BENBUN.Properties.Resources.webBaseLive;
                }
            }
        }
    }
}
